/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "detect_task.h"
#include "calibrate_task.h"
#include "obtein_mineral_task.h"
#include "voltage_task.h"
#include "led_flow_task.h"
#include "test_task.h"
#include "ins_task.h"
#include "gimbal_task.h"

#include "referee_usart_task.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
osThreadId TaskHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
osThreadId detect_handle;
osThreadId calibrate_tast_handle;
osThreadId obtein_mineral_handle;
osThreadId battery_voltage_handle;
osThreadId led_flow_Handle;
osThreadId task_Handle;
osThreadId imuTask_Handle;
osThreadId gimbal_handle;
osThreadId exchange_handle;

osThreadId referee_usart_task_handle;
/* USER CODE END FunctionPrototypes */

void tast_Task(void const * argument);

extern void MX_USB_DEVICE_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* GetTimerTaskMemory prototype (linked to static allocation support) */
void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize );

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/* USER CODE BEGIN GET_TIMER_TASK_MEMORY */
static StaticTask_t xTimerTaskTCBBuffer;
static StackType_t xTimerStack[configTIMER_TASK_STACK_DEPTH];

void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
{
  *ppxTimerTaskTCBBuffer = &xTimerTaskTCBBuffer;
  *ppxTimerTaskStackBuffer = &xTimerStack[0];
  *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
  /* place for user code */
}
/* USER CODE END GET_TIMER_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of Task */
  osThreadDef(Task, tast_Task, osPriorityNormal, 0, 128);
  TaskHandle = osThreadCreate(osThread(Task), NULL);

  /* USER CODE BEGIN RTOS_THREADS */

  osThreadDef(task, test_task, osPriorityNormal, 0, 128);         //瀹岀粨
  task_Handle = osThreadCreate(osThread(task), NULL);
	
  //绂荤嚎妫?鏌?
  osThreadDef(detect, detect_task, osPriorityNormal, 0, 256);     //宸插垵姝ュ畬缁?
  detect_handle = osThreadCreate(osThread(detect), NULL);

  //鏍″噯浠诲姟
  osThreadDef(cali, calibrate_task, osPriorityNormal, 0, 512);    //鍒濇瀹屾垚澶瑰叿鐨勬牎鍑?
  calibrate_tast_handle = osThreadCreate(osThread(cali), NULL);

  //澶瑰彇鐭跨煶
  osThreadDef(mineral, obtein_mineral_task, osPriorityNormal, 0, 512);   //鍩烘湰瀹屾垚锛屽叿浣撳弬鏁板緟瀹?
  obtein_mineral_handle = osThreadCreate(osThread(mineral), NULL);
	
  //浜戝彴浠诲姟
  osThreadDef(gimbal, gimbal_task, osPriorityNormal, 0, 256);            //鍒濇瀹屾垚锛屽叿浣撳緟瀹?
  gimbal_handle = osThreadCreate(osThread(gimbal), NULL);

// 鐢垫簮鐢靛帇閲囬泦
//  osThreadDef(BATTERY_VOLTAGE, battery_voltage_task, osPriorityNormal, 0, 128);   //寰呭畬缁?
//  battery_voltage_handle = osThreadCreate(osThread(BATTERY_VOLTAGE), NULL);
	
    osThreadDef(REFEREE, referee_usart_task, osPriorityNormal, 0, 256);
    referee_usart_task_handle = osThreadCreate(osThread(REFEREE), NULL);
  //蹇冭烦浠诲姟
  osThreadDef(led, led_flow_task, osPriorityNormal, 0, 256);                       //by 棰滅鍏? 
  led_flow_Handle = osThreadCreate(osThread(led), NULL);
	
  //IMU
  // osThreadDef(imuTask, INS_task, osPriorityRealtime, 0, 1024);                    //宸插垵姝ュ畬鎴愶紝uint16绫诲瀷鏁版嵁
  // imuTask_Handle = osThreadCreate(osThread(imuTask), NULL);

  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_tast_Task */
/**
  * @brief  Function implementing the Task thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_tast_Task */
__weak void tast_Task(void const * argument)
{
  /* init code for USB_DEVICE */
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN tast_Task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END tast_Task */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */
